<?php
include 'admin_passport_permission_lib.php';

$a_css_files = array (
    array ( 'path' => 'plugins/jquery-ui/jquery-ui-1.10.1.custom.min.css' ),
    array ( 'path' => 'plugins/bootstrap-modal/css/bootstrap-modal.css' ),
    array ( 'path' => 'plugins/chosen-bootstrap/chosen/chosen.css' ),
);

$a_js_files = array (
    array ( 'path' => 'plugins/jquery-file-upload/js/vendor/tmpl.min.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modal.js' ),
    array ( 'path' => 'plugins/bootstrap-modal/js/bootstrap-modalmanager.js' ),
    array ( 'path' => 'plugins/chosen-bootstrap/chosen/chosen.jquery.min.js' ),
    array ( 'path' => 'scripts/nh-admin-passport-permission.js' ),
);

$a_js_class[] = 'Admin_Permission.init();';

// <editor-fold defaultstate="collapsed" desc="Get Data">
$a_users = $a_modules_actions = $a_modules = $a_moduleactionid = $a_permissions = array ( );

make_where($s_Where, $a_filterings);

$db_query  = db_get_query($a_tablecolumns_namhoa_passport_module);
$db_query  = $db_query . $s_Where;
$db_result = db_query($db_conn, $db_query);
    
while ($d_row = db_fetch($db_result)) {
    $a_modules[$d_row->moduleid] = $d_row;
}

$db_query  = "select moduleid, actionid, moduleactionid from passport_module_action where moduleid in ('" . implode("','", array_keys($a_modules)) . "')";
$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    $a_modules_actions[$d_row->moduleid][] = $d_row;
    $a_moduleactionid                   [] = $d_row->moduleactionid;
}

if(!empty($a_moduleactionid)) {
    
    $db_query = "
        select  permissionid, moduleactionid, permissionfor, permissionfortype, permissionstatus
        from    passport_permission
        where   moduleactionid in (". implode(", ", $a_moduleactionid) .") and permissiontype = 'ALLOW' ";
    
    $db_result = db_query($db_conn, $db_query);

    while ($d_row = db_fetch($db_result)){
        
        if ($d_row->permissionfortype == 'USER') {
            $a_users[$d_row->permissionfor] = $d_row->permissionfor;
        }
        
        $a_permissions[$d_row->moduleactionid][$d_row->permissionfortype][$d_row->permissionfor] = $d_row->permissionfor;
    }

    // <editor-fold defaultstate="collapsed" desc="Set User Information">
    $a_users = get_user_info($a_users, $db_conn);

    foreach ($a_permissions as $key1 => $a_value2) {
        foreach ($a_value2 as $key2 => $a_value3) {
            if ($key2 == 'GROUP') {
                foreach ($a_value3 as $key3 => $value) {
                    $a_permissions[$key1][$key2][$key3] = isset($a_PhanBan[$value]) ? $a_PhanBan[$value] : $value;
                }
            } else {
                foreach ($a_value3 as $key3 => $value) {
                    
                    $a_permissions[$key1][$key2][$key3] = $a_users[$value]->saint . ' ' . $a_users[$value]->username;
                }
            }
        }
    }
    // </editor-fold>
}

$a_permission = get_passport_action_from_module($db_conn, array('PHAN_QUYEN'));
// </editor-fold>

include 'lib_head.php';
?>
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div id="portlet-config" class="modal hide">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"></button>
        <h3>Bộ Lọc</h3>
    </div>
    <div class="modal-body">
        Điền thông tin cần thiết để lọc được dữ liệu thích hợp.
    </div>
</div>
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div class="row-fluid">
    <!-- BEGIN BORDERED TABLE PORTLET-->
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="icon-filter"></i>Lọc Dữ Liệu</div>
            <div class="tools">
                <a href="javascript:;" class="collapse"></a>
                <a href="#portlet-config" data-toggle="modal" class="config"></a>
            </div>
        </div>
        <div class="portlet-body">
            <form action="" style="margin: 0px;">
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <div class="controls">
                            <div class="input-prepend">
                                <div class="btn-group">
                                    <select name="field1" class="m-wrap span12">
                                        <option value="modulename">Tên Chức Năng</option>
                                    </select>
                                </div>
                                <input class="m-wrap medium span12" name="keyword1" type="text" value="<?=check_plain($s_keyword1)?>"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="pull-left">
                    <div class="control-group">
                        <div class="controls">
                            <div class="input-prepend">
                                <button class="btn blue">BẮT ĐẦU LỌC <i class="m-icon-swapright m-icon-white"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </form>
        </div>
    </div>
    <!-- END BORDERED TABLE PORTLET-->
    <div class="portlet-body">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th style="text-align: center;">Chức Năng</th>
                    <th style="text-align: center;">Thao Tác</th>
                    <th style="text-align: center;">Nhóm</th>
                    <th style="text-align: center;">Người Dùng</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                foreach($a_modules_actions as $v_moduleid => $a_actions) {

                    $v_tmp_total  = count($a_actions);
                    $a_tmp_action = array_pop($a_actions);

                    $v_tmp_modulename = $a_modules[$v_moduleid]->modulename;
                    $v_tmp_actionname = isset($a_passport_actions[$a_tmp_action->actionid]) ? $a_passport_actions[$a_tmp_action->actionid] : $a_tmp_action->actionid;
                ?>
                <tr moduleactionid="<?=$a_tmp_action->moduleactionid?>" class="<?=$v_moduleid == 'ADMIN' ? 'hidden' : ''?>">
                    <td rowspan="<?=$v_tmp_total?>">
                        <strong><?=$v_tmp_modulename?></strong>
                    </td>
                    <td><?=$v_tmp_actionname?></td>
                    <td>
                        <a class="btn mini yellow-stripe group <?=in_array('update-permission',$a_permission)?'':'hidden'?>">
                            <i class="icon-group"></i> Cập Nhật
                        </a>
                        <div class="result">
                            <?= show_data_list($a_permissions[$a_tmp_action->moduleactionid]['GROUP' ]) ?>
                        </div>
                    </td>
                    <td>
                        <a class="btn mini yellow-stripe user <?=in_array('update-permission',$a_permission)?'':'hidden'?>">
                            <i class="icon-user"></i> Cập Nhật
                        </a>
                        <div class="result">
                            <?= show_data_list($a_permissions[$a_tmp_action->moduleactionid]['USER' ]) ?>
                        </div>
                    </td>
                </tr>
                <?php
                while (count($a_actions)) {
                    $a_tmp_action     = array_pop($a_actions);
                    $v_tmp_actionname = isset($a_passport_actions[$a_tmp_action->actionid]) ? $a_passport_actions[$a_tmp_action->actionid] : $a_tmp_action->actionid;
                ?>
                <tr moduleactionid="<?=$a_tmp_action->moduleactionid?>">
                    <td><?=$v_tmp_actionname?></td>
                    <td>
                        <a class="btn mini yellow-stripe group <?=in_array('update-permission',$a_permission)?'':'hidden'?>">
                            <i class="icon-group"></i> Cập Nhật
                        </a>
                        <div class="result">
                            <?= show_data_list($a_permissions[$a_tmp_action->moduleactionid]['GROUP' ]) ?>
                        </div>
                    </td>
                    <td>
                        <a class="btn mini yellow-stripe user <?=in_array('update-permission',$a_permission)?'':'hidden'?>">
                            <i class="icon-user"></i> Cập Nhật
                        </a>
                        <div class="result">
                            <?= show_data_list($a_permissions[$a_tmp_action->moduleactionid]['USER' ]) ?>
                        </div>
                    </td>
                </tr>
                <?php
                    }
                }
                ?>
            </tbody>
        </table>
    </div>
</div>
<div id="responsive" class="modal hide fade" tabindex="-1">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h3>Cập Nhật Thông Tin</h3>
    </div>
    <div class="modal-body">
        <input type="hidden" name="moduleactionid" value=""> 
        <input type="hidden" name="permissionfortype" value=""> 
        <div class="row-fluid">
            <div>
                <div class="control-group">
                    <div class="controls">
                        <select name="permissionfor" data-placeholder="Chọn ..." class="chosen span12 chosen-rtl" multiple="multiple" tabindex="6" style="width: 500px">
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn">Thoát</button>
        <button type="button" class="btn blue btn-save">Lưu</button>
    </div>
</div>
<script id="item-tmpl" type="text/x-tmpl">
    {% for (var i=0, item_info; item_info = o.data[i]; i++) { %}
        <option {% if(item_info.checked == true) { %} {%="selected"%}  {% } %} value="{%=item_info.itemid%}">{%=item_info.itemname%}</option>
    {% } %}
</script>
<?php include 'lib_tail.php'; ?>